const http = require('../../../utils/http.js')
const util = require('../../../utils/util.js')

var date = new Date()
var years = []
var months = []
var days = []

for (let i = date.getFullYear(); i <= date.getFullYear() + 5; i++) {
    years.push(i)
}
for (let i = 1; i <= 12; i++) {
    months.push(util.formatNumber(i))
}
for (let i = 1; i <= 31; i++) {
    days.push(util.formatNumber(i))
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        navBar: {
            title: '数据服务',
            btn: 'goBack'
        },
        chooseTime: false,
        chooseTimeEnd: false,
        years: years,
        months: months,
        days: days,
        value: [0, date.getMonth(), date.getDate() - 1]
    },

    /**
     * 返回
     */
    goBack() {
        http.goBack()
    },

    /**
     * 开启选择开始时间
     */
    openChooseTime() {
        let chooseTime = this.data.chooseTime
        if (chooseTime) {
            this.setData({
                chooseTime: false
            })
        } else {
            this.setData({
                chooseTime: true
            })
        }
    },

    /**
     * 关闭选择开始时间
     */
    closeChooseTime() {
        this.setData({
            chooseTime: false
        })
    },

    /**
     *  选择开始时间
     */
    bindChange(e) {
        const val = e.detail.value
        let year = this.data.years[val[0]]
        let month = this.data.months[val[1]]
        days = []
        if (year % 4 == 0) {
            if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                for (var i = 1; i <= 31; i++) {
                    days.push(i)
                }
            } else if (month == '02') {
                for (var i = 1; i <= 29; i++) {
                    days.push(i)
                }
            } else {
                for (var i = 1; i <= 30; i++) {
                    days.push(i)
                }
            }
        } else {
            if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
                for (var i = 1; i <= 31; i++) {
                    days.push(i)
                }
            } else if (month == '02') {
                for (var i = 1; i <= 28; i++) {
                    days.push(i)
                }
            } else {
                for (var i = 1; i <= 30; i++) {
                    days.push(i)
                }
            }
        }
        this.setData({
            days: days,
            value: val
        })
    },

    /**
     * 获取店铺详情
     */
    getShopData(token, shop_id, type_id, sdate, edate) {
        let that = this,
            url = 'Shopdata_getShopdataList',
            sdateArr = sdate.split('-'),
            edateArr = edate.split('-'),
            years = this.data.years,
            months = this.data.months,
            days = this.data.days

        http._post(
            url,
            { token: token, shop_id: shop_id, type_id: type_id, sdate: sdate, edate: edate },
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    wx.showLoading()
                    let shop_list = data.data.shop_list
                    for (let i = 0; i < shop_list.length; i++) {
                        let item_arr = shop_list[i].split('：')
                        shop_list[i] = item_arr
                    }

                    let timeCount = setInterval(function () {
                        if (shop_list[shop_list.length - 1].length === 2) {
                            that.setData({
                                shop: data.data,
                                shopList: shop_list,
                                sValue: [years.indexOf(Number(sdateArr[0])), months.indexOf(sdateArr[1]), days.indexOf(sdateArr[2])],
                                eValue: [years.indexOf(Number(edateArr[0])), months.indexOf(edateArr[1]), days.indexOf(edateArr[2])]
                            })
                            clearInterval(timeCount)
                            wx.hideLoading()
                        }
                    }, 500)
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let token = wx.getStorageSync('token'),
            shop_id = options.shop_id,
            type_id = options.type_id,
            sdate = options.sdate,
            edate = options.edate
            
        this.getShopData(token, shop_id, type_id, sdate, edate)  // 获取店铺详情
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})